package top.maof.book.spider.develop.contents.dingdian;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;

/**
 * 顶点小说
 * 可用,短时间内不能多次访问
 */

@Data
@Slf4j
@TargetUrl("https://www.ddxs.cc/ddxs/\\d+")
@HelpUrl({"https://www.ddxs.cc/sort/xiuzhen/\\d+.html","https://www.ddxs.cc/sort/xuanhuan/\\d+.html",
"https://www.ddxs.cc/sort/dushi/\\d+.html","https://www.ddxs.cc/sort/wangyou/\\d+.html",
        "https://www.ddxs.cc/sort/kehuan/\\d+.html","https://www.ddxs.cc/wanben/sort/2.html"})
public class DDXS implements AfterExtractor, Split {
    public static final String PROTOCOL = "https";

    public static final String URL = "https://www.ddxs.cc";

    @ExtractBy(value = "//div[@id=maininfo]/div[@id=info]/h1/text()")
    private String name;

    @ExtractBy(value = "//div[@id=maininfo]/div[@id=info]/p[1]/text()")
    private String author;
    //作    者：忘语

    @ExtractBy(value = "//div[@id=fmimg]/img/@src")
    private String img;

    @ExtractBy(value = "//div[@id=maininfo]/div[@id=info]/p[2]/text()")
    private String classify;
    //类 别： 修真小说

    private Date updateDate;

    private Date insertDate = new Date();

    @ExtractBy(value = "//div[@id=intro]/text()")
    private String synopsis;

    private String state;

    private String url;

    // @ExtractBy(value = "//div[@id=list]")
    private String content;

    private Integer webId = 9;

    public static final String CHARSET = "UTF-8";

    @Override
    public Book getBook() {
        Book book = new Book();
        book.setAuthor(this.author);
        book.setName(this.name);
//        book.setState(this.state);
//        book.setImg(this.img);
//        book.setInsertDate(this.insertDate);
//        book.setClassify(this.classify);
//        book.setSynopsis(this.synopsis);
        // book.setUpdateDate(this.updateDate);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        // contents.setUpdateDate(this.updateDate);
        return contents;
    }


    @Override
    public void afterProcess(Page page) {
      /*  //获取最新章节更新时间   2019-04-17 02:11:06
        String updateStr = page.getHtml().xpath("//div[@id=maininfo]/div[@id=info]/p[4]/time/text()").toString().substring(7, 17).trim();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
        try {
            this.updateDate = simpleDateFormat.parse(updateStr);
            Date today = new Date();
            //如果最近更新时间和抓取当天时间相隔一个月,便默认更新完毕或已断更,保存html
            if ((today.getTime() - this.updateDate.getTime()) > 1000 * 60 * 60 * 24 * 30L) {
                //this.content = page.getHtml().xpath("//div[@id=list]").toString();
                this.state = "完本";
            } else {
                //this.state = "连载";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        // 对得到的作者字段做处理  作    者：忘语
        this.author = this.author.trim();

        log.info(this.name + "===" + this.author);

        this.author = this.author.substring(this.author.indexOf("：") + 1).trim();

        // 类 别： 修真小说
//        this.classify = this.classify.trim();
//        this.classify = this.classify.substring(this.classify.indexOf("：")).trim();

        // img字段做处理
        /*if (!this.img.startsWith("http")) {
            if (this.img.startsWith("//")) {
                this.img = PROTOCOL + ":" + this.img;
            } else if (this.img.startsWith("/"))
                this.img = URL + this.img;
            else {
                this.img = page.getUrl().toString().substring(0, page.getUrl().toString().lastIndexOf("/") + 1) + this.img;
            }
        }*/

//        this.content = page.getHtml().toString();

        //获取该页面url
        this.setUrl(page.getRequest().getUrl());
    }

}

